package cn.kzgg.org.controller;


import cn.kzgg.basic.anno.PreAuthorize;
import cn.kzgg.basic.util.AjaxResult;
import cn.kzgg.basic.util.PageList;
import cn.kzgg.org.domain.Department;
import cn.kzgg.org.query.DepartmentQuery;
import cn.kzgg.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 按id查
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@PathVariable("id") Long id){
       return departmentService.findById(id);
    }

    /**
     * 查所有
     * @param
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查所有" )
    public List<Department> queryAll(){
        return departmentService.findAll();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @PreAuthorize(name="删除数据",value = "dept:deleteById")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("网络繁忙");
        }
    }

    /**
     * 新增修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.save(department);

            }else{
                departmentService.update(department);
            }
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setMessage("网络繁忙").setSuccess(false);
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.show();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.show().setSuccess(false).setMessage("网络繁忙");
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "无极数" )
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }

}
